﻿"use strict";
define(['admin/app', "service/baseService", "service/admin/roleService"], function (app) {
    app.controller("roleController", function ($scope, $state, $stateParams, $location, utils, baseService, roleService, w5cValidator) {
        var methods = {
            search: function () {
                roleService.gets({ name: $scope.name }).success(function (response) { $scope.model = response.data; });
            },
            edit: function () {
                if ($stateParams.id > 0) {
                    roleService.get({ id: $stateParams.id }).success(function (response) {
                        $scope.model = response.data;
                    });
                    $scope.breadcrumb = '编辑';
                }
                else {
                    $scope.model = { id: 0, feature_ids: [] };
                    $scope.breadcrumb = '新增';
                }
                w5cValidator.setRules({
                    name: { required: '请填写权限名称' }
                });
            },
            goBack: function () {
                history.back();
            },
            save: function () {
                var model = $scope.model;
                $scope.psdisabled = true;
                if (model.id == 0) {
                    roleService.post(model).success(function (response) {
                        utils.notify(response.result_msg, response.result_code == 1000 ? "success" : "warning");
                        if (response.result_code == 1000)
                            methods.goBack();
                        else
                            $scope.psdisabled = false;
                    }).error(function () { $scope.psdisabled = false; });
                } else {
                    roleService.put(model).success(function (response) {
                        utils.notify(response.result_msg, response.result_code == 1000 ? "success" : "warning");
                        if (response.result_code == 1000)
                            methods.goBack();
                        else
                            $scope.psdisabled = false;
                    }).error(function () { $scope.psdisabled = false; });
                }
            },
            remove: function (id) {
                var modal = utils.confirm({ msg: "是否需要删除当前项目", ok: "确定", cancel: "取消" });
                modal.result.then(function () {
                    roleService.delete(id).success(function () {
                        methods.search();
                    });
                })
            },
            admin_menus: function () {
                baseService.admin_menus().success(function (response) {
                    $scope.meuns = [];
                    $.each(response.data, function (index, item) {
                        $.each(item.children, function (index, xitem) {
                            $scope.meuns.push(xitem);
                        });
                    });
                });
            },
            choose: function ($event, id) {
                var checkbox = $event.target;
                var checked = checkbox.checked;
                var name = checkbox.name;
                var idx = $scope.model.feature_ids.indexOf(id);
                if (checked && idx == -1) {
                    $scope.model.feature_ids.push(id);
                }
                else if (!checked && idx != -1) {
                    $scope.model.feature_ids.splice(idx, 1);
                }
            }
        }
        angular.extend($scope, methods);
        if ($stateParams.action != undefined) {
            methods.edit();
            methods.admin_menus();
        }
        else
            methods.search();
    });
});
